package com.paipaideli.common.bottomsheet;

import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.jakewharton.rxbinding2.view.RxView;
import com.paipaideli.R;
import com.paipaideli.common.utils.ToastUtil;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.functions.Consumer;

/**
 * Created by wangjia on 2017/5/9.
 */

public class ShareDialogFragment extends BasePresenterBottomSheetDialogFragment implements UMShareListener {

    @BindView(R.id.lin_wxhy)//微信
            LinearLayout llWechat;
    @BindView(R.id.lin_wxpyq)//朋友圈
            LinearLayout llMoment;
    @BindView(R.id.lin_qqhy)//qq
            LinearLayout llQq;
    @BindView(R.id.lin_qqkj)//qq空间
            LinearLayout llQqzone;
    @BindView(R.id.lin_cancel)//取消
            LinearLayout lincancel;
    Unbinder unbinder;


    private ShareEventListener shareEventListener;
    private ShareAction mShareAction;
    private UMShareListener mUMShareListener;

    public static ShareDialogFragment newInstance() {
        return new ShareDialogFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.pupopwindow_shar, container, false);
        unbinder = ButterKnife.bind(this, view);

        RxView.clicks(lincancel)
                .subscribe(o -> dismiss(), throwable -> Log.e(TAG, "error", throwable));

        RxView.clicks(llWechat)
                .subscribe(o -> {
                    onShare(SHARE_MEDIA.WEIXIN);
                    if (consumer != null) {
                        consumer.accept("");
                    }
                    dismiss();
                }, this::showError);

        RxView.clicks(llMoment)
                .subscribe(o -> {
                    onShare(SHARE_MEDIA.WEIXIN_CIRCLE);
                    if (consumer != null) {
                        consumer.accept("");
                    }
                    dismiss();
                }, this::showError);

        RxView.clicks(llQq)
                .subscribe(o -> {
                    onShare(SHARE_MEDIA.QQ);
                    if (consumer != null) {
                        consumer.accept("");
                    }
                    dismiss();
                }, this::showError);

        RxView.clicks(llQqzone)
                .subscribe(o -> {
                    onShare(SHARE_MEDIA.QZONE);
                    if (consumer != null) {
                        consumer.accept("");
                    }
                    dismiss();
                }, this::showError);
        return view;
    }

    public void setShareAction(ShareAction shareAction) {
        this.mShareAction = shareAction;
    }

    public void setUMShareListener(UMShareListener uMShareListener) {
        this.mUMShareListener = uMShareListener;
    }

    private void showError(Throwable throwable) {
        Log.e(TAG, "error", throwable);
    }

    private void onShare(SHARE_MEDIA shareMedia) {
        if (shareEventListener != null) {
            shareEventListener.onShare(shareMedia);
        } else if (mShareAction != null) {
            mShareAction.setPlatform(shareMedia);
            share(mShareAction, mUMShareListener);
        }
    }

    public void share(ShareAction shareAction, UMShareListener listener) {
        if (listener == null) {
            shareAction.setCallback(this);
        } else {
            shareAction.setCallback(listener);
        }
        shareAction.share();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    public void setShareEventListener(ShareEventListener shareEventListener) {
        this.shareEventListener = shareEventListener;
    }

    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA shareMedia) {
        if (shareMedia.name().equals("WEIXIN_FAVORITE")) {
            ToastUtil.show("收藏成功");
        } else {
            ToastUtil.show("分享成功");
        }
    }

    @Override
    public void onError(SHARE_MEDIA shareMedia, Throwable throwable) {
        ToastUtil.show("分享失败");
        Log.e(TAG, "error", throwable);
    }

    @Override
    public void onCancel(SHARE_MEDIA shareMedia) {
        ToastUtil.show("分享取消");
    }

    public interface ShareEventListener {

        void onShare(SHARE_MEDIA shareMedia);
    }

    private Consumer<String> consumer;

    public void onClicks(Consumer<String> consumer) {
        this.consumer = consumer;
    }

}
